<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
        <p>doneTodos:{{doneTodos}}</p>
        <p>doneTodosCount:{{doneTodosCount}}</p>
        <p>getTodoById:{{getTodoById(2)}}</p>
    </div>
    <script src="vue.js"></script>
    <script src="my-vuex.js"></script>
    <script>
        Vue.use(Vuex);
        const store = new Vuex.Store({
            state:{
                todos:[
                    {id:1,text:'吃饭',done:true},
                    {id:2,text:'睡觉',done:true},
                    {id:3,text:'玩',done:false}
                ]
            },//store的计算属性，或者叫访问器
            getters:{
                 //获取已经完成的todo数组
                doneTodos(state){
                  return state.todos.filter(todo=>todo.done);  
                },
                //获取已经完成的todo数组的数量
                doneTodosCount(state,getters){
                  return getters.doneTodos.length;  
                },
                getTodoById(state){
                    return function(id){
                        return state.todos.find(todo=>todo.id === id);
                    }
                } 
               
            }
        })
        var vm = new Vue({
            el:'#app',
            store,
            computed:{
                //把仓库中的getters映射为Vue实例的计算属性，以便在模板中使用
                ...Vuex.mapGetters(['doneTodos','doneTodosCount','getTodoById'])
            }
        });
    </script>
  </body>
</html>
